package com.lykj.llw.box.custom.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.blankj.utilcode.util.ColorUtils

/**
 * ================================================
 * 自定义圆角进度条
 * ================================================
 *
 * @author gosha
 * @date 2023/12/18-16:57
 */
class CustomCornerProgressBar : View {
    private var mPaint: Paint? = null
    private var mBackgroundPaint: Paint? = null
    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private var mProgress: Int = 0

    constructor(context: Context?) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)

    init {
        mPaint = Paint()
        mPaint?.apply {
            isAntiAlias = true
            style = Paint.Style.FILL
            color = ColorUtils.string2Int("#FF664FC3")
            strokeWidth = mHeight.toFloat()
        }

        mBackgroundPaint = Paint()
        mBackgroundPaint?.apply {
            isAntiAlias = true
            style = Paint.Style.FILL
            color = ColorUtils.string2Int("#cbcbcb")
            strokeWidth = mHeight.toFloat()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        mWidth = MeasureSpec.getSize(widthMeasureSpec)
        mHeight = MeasureSpec.getSize(heightMeasureSpec)

//        Log.e(
//            "AAA",
//            "width:${MeasureSpec.getSize(widthMeasureSpec)} - height:${
//                MeasureSpec.getSize(heightMeasureSpec)
//            }"
//        )
//
//        Log.e("BBB", "width:$mWidth - height:$mHeight")
    }

    open fun setProgress(progress: Int) {
        this.mProgress = progress
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawRoundRect(
            0f,
            0f,
            mWidth.toFloat(),
            mHeight.toFloat(),
            mHeight.toFloat(),
            mHeight.toFloat(),
            mBackgroundPaint!!
        )

        //裁切
        var path = Path()
        path.addRoundRect(
            0f,
            0f,
            mWidth.toFloat(),
            mHeight.toFloat(),
            mHeight.toFloat(),
            mHeight.toFloat(),
            Path.Direction.CW
        )
        canvas.clipPath(path)

        //绘制进度
        canvas.drawRoundRect(
            0f,
            0f,
            mWidth.toFloat() * mProgress / 100,
            mHeight.toFloat(),
            0f,
            0f,
            mPaint!!
        )
    }
}